package com.symaster.common.swing.dialog;

import com.symaster.common.image.util.ImageUtil;
import com.symaster.common.swing.element.ImagePanel;
import com.symaster.common.swing.util.SwingUtil;
import org.opencv.core.Mat;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class ShowImageDialog extends JDialog {
    private JPanel contentPane;

    public ShowImageDialog(BufferedImage image) {
        initPage();
        showImage(image);
    }

    public ShowImageDialog(Image image) {
        initPage();
        showImage(image);
    }

    public ShowImageDialog(String imagePath) {
        initPage();
        showImage(imagePath);
    }

    public ShowImageDialog(Mat mat) {
        initPage();
        showImage(mat);
    }

    public static void main(String[] args) throws Exception {

        Image read = ImageIO.read(new File("D:\\server-icon.png"));
        ShowImageDialog dialog = new ShowImageDialog(read);
        dialog.open();
        System.exit(0);
    }

    private void initPage() {
        setTitle("查看图片");
        JPanel jPanel = new JPanel();
        jPanel.setLayout(new BorderLayout());
        jPanel.add(contentPane, BorderLayout.CENTER);

        Dimension hd = new Dimension(0, 10);
        Dimension wd = new Dimension(10, 0);

        JPanel jPanel1 = new JPanel();
        JPanel jPanel2 = new JPanel();
        jPanel.add(jPanel1, BorderLayout.SOUTH);
        jPanel.add(jPanel2, BorderLayout.NORTH);
        jPanel1.setPreferredSize(hd);
        jPanel2.setPreferredSize(hd);

        JPanel jPanel3 = new JPanel();
        JPanel jPanel4 = new JPanel();
        jPanel.add(jPanel3, BorderLayout.EAST);
        jPanel.add(jPanel4, BorderLayout.WEST);
        jPanel3.setPreferredSize(wd);
        jPanel4.setPreferredSize(wd);

        setContentPane(jPanel);
        setPreferredSize(new Dimension(600, 600));
        setModal(true);
        pack();
        SwingUtil.toCenter(this);
    }

    private void showImage(Mat mat) {
        ImageUtil.scaleMat(mat, 500);
        showImage(ImageUtil.toBufferedImage(mat));
    }

    private void showImage(String imagePath) {
        Mat mat = null;
        try {
            FileInputStream fileInputStream = new FileInputStream(imagePath);
            mat = ImageUtil.toMat(fileInputStream);
            showImage(mat);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } finally {
            if (mat != null) {
                mat.release();
            }
        }
    }

    private void showImage(Image image) {
        contentPane.add(new ImagePanel(image, ImagePanel.SHOW_TYPE_CENTER));
    }

    private void showImage(BufferedImage image) {
        contentPane.add(new ImagePanel(image, ImagePanel.SHOW_TYPE_CENTER));
    }

    public void open() {
        setVisible(true);
    }

    @Override
    public void setBackground(Color bgColor) {
        super.setBackground(bgColor);
        if (contentPane != null) {
            contentPane.setBackground(bgColor);
        }
    }

    {
        // GUI initializer generated by IntelliJ IDEA GUI Designer
        // >>> IMPORTANT!! <<<
        // DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout(0, 0));
    }

    /** @noinspection ALL */
    public JComponent $$$getRootComponent$$$() {
        return contentPane;
    }

}
